﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Globalization;

namespace GroundStation
{
    class PlaneValues
    {
        public enum ValueType
        {
            Number,
            Text,
            Boolean
        };

        private UIElement UiParent;
        private TextBox ValueLab;
        private ProgressBar ProgBar;
        public string Unit;
        public int SrcPosition;
        public string Format = "f";
        private string _value = "";
        public string Name;
        public ValueType Type;
        public double MinValue;
        public double MaxValue;

        public string Value
        {
            get
            {
                return _value;
            }

            set
            {
                _value = value;
                Refresh();
            }
        }

        public PlaneValues(StackPanel ParentUIElement, string Name, string Unit, ValueType Type, int SrcPosition)
        {
            NewPlaneValues(ParentUIElement, Name, Unit, Type, SrcPosition);
        }

        public PlaneValues(StackPanel ParentUIElement, string Name, string Unit, ValueType Type, int SrcPosition, double Min, double Max)
        {
            MinValue = Min;
            MaxValue = Max;
            NewPlaneValues(ParentUIElement, Name, Unit, Type, SrcPosition);
        }

        public void NewPlaneValues(StackPanel ParentUIElement, string Name, string Unit, ValueType Type, int SrcPosition)
        {
            UiParent = ParentUIElement;
            this.Name = Name;
            this.Unit = Unit;
            this.SrcPosition = SrcPosition;

            Canvas Cont = new Canvas();
            Label Lab = new Label();
            ValueLab = new TextBox();
            if (MinValue != MaxValue)
            {
                ProgBar = new ProgressBar();
                //ProgBar.Minimum = MinValue;
                //ProgBar.Maximum = MaxValue;
                ProgBar.Margin = new Thickness(250, 5, 0, 0);
                ProgBar.Height = 20;
                ProgBar.Width = 200;
                Cont.Children.Add(ProgBar);
            }
            Cont.Height = 32;
            Lab.Content = Name;
            ValueLab.IsReadOnly = true;
            ValueLab.BorderThickness = new Thickness(0, 0, 0, 0);
            Lab.Margin = new Thickness(0, 0, 0, 0);
            ValueLab.Margin = new Thickness(150, 5, 0, 0);
            Refresh();

            Cont.Children.Add(Lab);
            Cont.Children.Add(ValueLab);
            ParentUIElement.Children.Add(Cont);
        }

        private void Refresh()
        {
            double number;
            double nnumb;

            double.TryParse(Value, System.Globalization.NumberStyles.Float, CultureInfo.InvariantCulture, out number);

            ValueLab.Text = Value + " " + Unit;

            nnumb = (number - MinValue) / (MaxValue - MinValue);

            if (nnumb > 1) nnumb = 1;
            if (nnumb < 0) nnumb = 0;

            if (MinValue != MaxValue) ProgBar.Value  = nnumb * 100;
        }
    }
}
